import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/template/event'

const api = {

    /**
     * 新增事件定义
     * @param event
     * @returns {Promise<*|undefined>}
     */
    createEvent: (event) =>
      axiosClient().post(`${requestUrl}/addInfo`, event),

    /**
     * 根据ID删除事件定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteEventById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新事件定义
     * @param id
     * @param event
     * @returns {Promise<*|undefined>}
     */
    updateEventFull: (id, event) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, event),

    /**
     * 根据ID部分更新事件定义
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateEventPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询事件定义
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getEventById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询事件定义
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getEventPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询事件定义
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getEventList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.processId) {
            params.append('processId', businessParam.processId)
        }
        if (businessParam.nodeId) {
            params.append('nodeId', businessParam.nodeId)
        }
        if (businessParam.eventCode) {
            params.append('eventCode', businessParam.eventCode)
        }
        if (businessParam.eventName) {
            params.append('eventName', businessParam.eventName)
        }
        if (businessParam.eventType) {
            params.append('eventType', businessParam.eventType)
        }
        if (businessParam.eventCondition) {
            params.append('eventCondition', businessParam.eventCondition)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
